package com.mytaobao.main.firsttab;


import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baseproject.adapter.BaseMultiItemDelegateAdapter;
import com.baseproject.adapter.ItemViewDelegate;
import com.baseproject.adapter.ItemViewDelegateManager;
import com.baseproject.adapter.ViewHolder;
import com.mytaobao.R;
import com.mytaobao.main.firsttab.func.bannertop.BannerTopItemDelagate;
import com.mytaobao.main.firsttab.func.chaoshihui.ChaoShiHuiItemDelegate;
import com.mytaobao.main.firsttab.func.flashsale.FlashSaleItemDelegate;
import com.mytaobao.main.firsttab.func.guessyoulike.GuessYouLikeItemDelegate;
import com.mytaobao.main.firsttab.func.guessyoulike.ItemGuessYouLike;
import com.mytaobao.main.firsttab.func.menu.MenuItemDelagate;
import com.mytaobao.main.firsttab.func.tianmaobiguang.TianMaoBiGuangItemDelegate;
import com.mytaobao.util.Const;

import java.util.List;

public class FirstTabRecyclerAdapter<T> extends BaseMultiItemDelegateAdapter implements View.OnClickListener, ItemViewDelegateManager.DelegateManagerCallBack {

    private static String TAG = "FirstTabRecyclerAdapter";

    private Fragment mFirstTabFragment;
    protected OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder,  int position);
        boolean onItemLongClick(View view, RecyclerView.ViewHolder holder,  int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void addItemViewDelegates() {
        mItemViewDelegateManager.addItemViewDelegate(new BannerTopItemDelagate());
        mItemViewDelegateManager.addItemViewDelegate(new MenuItemDelagate());
//        mItemViewDelegateManager.addItemViewDelegate(new HeadLineItemDelegate(mFirstTabFragment));
        mItemViewDelegateManager.addItemViewDelegate(new FlashSaleItemDelegate());
        mItemViewDelegateManager.addItemViewDelegate(new ChaoShiHuiItemDelegate());
        mItemViewDelegateManager.addItemViewDelegate(new TianMaoBiGuangItemDelegate());
        mItemViewDelegateManager.addItemViewDelegate(new GuessYouLikeItemDelegate(mFirstTabFragment));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public int getDelegateTypePosition(int position) {
        if(position < Const.TYPE_GUESS_YOU_LIKE) {
            ItemViewDelegate<T> delegate = mItemViewDelegateManager.getItemViewDelegate(position);
            return delegate.getItemViewType();
        } else {
            return Const.TYPE_GUESS_YOU_LIKE;
        }
    }

    @Override
    public int getWithinDelegateTypePosition(int position) {
        if (position < Const.TYPE_GUESS_YOU_LIKE) {
            return position;
        } else {
            return mItemViewDelegateManager.getItemViewDelegateTypeCount() - 1;
        }
    }


    public FirstTabRecyclerAdapter(FirstTabFragment mFirstTabFragment) {
        super(mFirstTabFragment, mFirstTabFragment.getContext());
        this.mFirstTabFragment = mFirstTabFragment;
    }

    /**
     * 决定元素的布局使用哪种类型
     * @param position 数据源的下标
     * @return 一个int型标志，传递给onCreateViewHolder的第二个参数
     */
    @Override
    public int getItemViewType(int position) {
        if (!useItemViewDelegateManager()) return super.getItemViewType(position);
        return mItemViewDelegateManager.getItemViewType(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "[onCreateViewHolder] viewType = " + viewType);
        ViewHolder holder = mItemViewDelegateManager.createViewHolder(mFirstTabFragment.getActivity(), parent, viewType);
        setListener(parent, holder, viewType);
        return holder;
    }

    protected boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateTypeCount() > 0;
    }

    protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) {

    }

    protected boolean isEnabled(int viewType) {
        return true;
    }

    @Override
    public int getItemCount() {
        return mItemViewDelegateManager.getItemViewCount();
    }

    public void setDataGuessYouLike(List<ItemGuessYouLike> itemGuessYouLikes) {
        mItemViewDelegateManager.updateData(Const.TYPE_GUESS_YOU_LIKE, itemGuessYouLikes);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tianmao:
                Toast.makeText(mFirstTabFragment.getActivity(), "天猫", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
